package com.palfund.nested;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by clvc on 2017/10/31.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */

public class MultiTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ONE = 0;
    private static final int TYPE_TWO = 1;
    private ArrayList<ArrayList<String>> mList = new ArrayList<>();
    private Context mContext;

    public MultiTypeAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("MultiTypeAdapter", "---onCreateViewHolder()--->   OutCreate" + viewType);
        switch (viewType) {
            case TYPE_ONE:

                return new TypeOneViewHolder(LayoutInflater.from(mContext).inflate(R.layout
                        .item_type1, parent, false));
            case TYPE_TWO:
                return new TypeTwoViewHolder(LayoutInflater.from(mContext).inflate(R.layout
                        .recyclerviewtop, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i("MultiTypeAdapter", "---onBindViewHolder()--->      OutBind" + position);
        TypeOneViewHolder typeOneViewHolder;
        TypeTwoViewHolder typeTwoViewHolder;
        if (holder instanceof TypeOneViewHolder) {
            typeOneViewHolder = (TypeOneViewHolder) holder;
            // TODO: 2017/10/31
            return;
        }
        if (holder instanceof TypeTwoViewHolder) {
            typeTwoViewHolder = (TypeTwoViewHolder) holder;
            RVAdapter rvAdapter = (RVAdapter) typeTwoViewHolder.mRecyclerView.getAdapter();
            rvAdapter.addAll(mList.get(position - 1), true);
            return;
        }
    }

    class TypeOneViewHolder extends RecyclerView.ViewHolder {
        private ViewPager mViewPager;
        private RecyclerView mRecyclerView;

        public TypeOneViewHolder(View itemView) {
            super(itemView);
            mViewPager = itemView.findViewById(R.id.viewPager);
            int[] pictureIds = {R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R
                    .drawable.p10, R.drawable.p11, R.drawable.p12, R.drawable.p13};
            ArrayList<ImageView> imageViews = new ArrayList<>();
            ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
            layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewPager.LayoutParams.MATCH_PARENT;
            for (int i = 0; i < pictureIds.length; i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setImageResource(pictureIds[i]);
                imageViews.add(imageView);
            }
            VPAdapter viewPagerAdapter = new VPAdapter(imageViews);
            mViewPager.setAdapter(viewPagerAdapter);
            mRecyclerView = itemView.findViewById(R.id.recyclerViewTop);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2,
                    GridLayoutManager.VERTICAL, false);
            mRecyclerView.setLayoutManager(gridLayoutManager);
            RVAdapter rvAdapterTop = new RVAdapter(mContext);
            rvAdapterTop.orientation = 0;
            mRecyclerView.setAdapter(rvAdapterTop);
            ArrayList<String> dataTop = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                dataTop.add("top   " + i);
            }
            rvAdapterTop.addAll(dataTop, false);
        }
    }

    class TypeTwoViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;

        public TypeTwoViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = itemView.findViewById(R.id.recyclerViewTop);
            GridLayoutManager manager = new GridLayoutManager(mContext, 2, GridLayoutManager
                    .HORIZONTAL, false);
            mRecyclerView.setLayoutManager(manager);
            RVAdapter rvAdapter = new RVAdapter(mContext);
            rvAdapter.orientation = 1;
            mRecyclerView.setAdapter(rvAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_ONE : TYPE_TWO;
    }

    public void addAll(ArrayList<ArrayList<String>> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }
}
